using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ZombiesCrowd : MonoBehaviour
{
	public Transform playerContainer;

	public float timeCrowd = 120f;

	public float wallRoom = 10f;

	public string sceneComplete;

	public ScreenTitleLevel titleLevel;

	public Text numZombies;

	private ManagerZombies[] managerZombies;

	private float[] startMinSpeed = new float[0];

	private Vector3 startPosition = default(Vector3);

	private float scaleTimer = 1f;

	private float currentTimerCrowd;

	private float markDistTimer;

	private void OnDrawGizmos()
	{
		if (base.enabled)
		{
			Gizmos.color = new Color(0f, 1f, 0.5f);
			Vector3 position = base.transform.position;
			Gizmos.DrawLine(position, new Vector3(position.x + wallRoom, position.y + 3f, position.y));
			Gizmos.DrawLine(new Vector3(position.x + wallRoom, position.y - 2f, position.y), new Vector3(position.x + wallRoom, position.y + 5f, position.y));
		}
	}

	private void Start()
	{
		base.transform.SetParent(playerContainer);
		startPosition = base.transform.position;
		managerZombies = base.gameObject.GetComponents<ManagerZombies>();
		startMinSpeed = new float[managerZombies.Length];
		for (int i = 0; i < managerZombies.Length; i++)
		{
			startMinSpeed[i] = managerZombies[i].scaleSpeedMove;
		}
		InvokeRepeating("CheckAlive", 10f, 4f);
		titleLevel.ShowStart();
		Player.listenersDeath = OnDeathPlayer;
	}

	private void Update()
	{
		if (Time.timeScale != 0f)
		{
			currentTimerCrowd += Time.deltaTime / Time.timeScale;
			scaleTimer = Mathf.Clamp01((timeCrowd - currentTimerCrowd) / timeCrowd);
			float a = scaleTimer;
			Vector3 position = base.transform.position;
			scaleTimer = Mathf.Min(a, 1f - Mathf.Clamp01((position.x - startPosition.x) / wallRoom));
		}
		if (currentTimerCrowd - markDistTimer > 0.2f)
		{
			markDistTimer = currentTimerCrowd;
			for (int i = 0; i < managerZombies.Length; i++)
			{
				managerZombies[i].periodScale = scaleTimer;
				managerZombies[i].scaleSpeedMove = Mathf.Lerp(managerZombies[i].scaleSpeedMoveMax, startMinSpeed[i], scaleTimer);
			}
		}
	}

	public void CheckAlive()
	{
		if (numZombies != null)
		{
			numZombies.text = ManagerZombies.aliveSceneTotal.ToString();
		}
		if (ManagerZombies.aliveSceneTotal == 0)
		{
			CancelInvoke("CheckAlive");
			titleLevel.ShowCompelete(LoadScene);
		}
	}

	public void OnDeathPlayer()
	{
		CancelInvoke("CheckAlive");
		Invoke("LoadScene", 4f);
	}

	public void LoadScene()
	{
		SettingsStartGame.ResultGame(ManagerZombies.kills, Player.onStage.scores, Mathf.CeilToInt(Player.onStage.moneyHits));
		SettingsStartGame.GameOver(resetWeapon: false);
		SceneManager.LoadScene(sceneComplete);
	}
}
